package org.groovymud.shell.security.loginsteps;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;

import org.groovymud.object.alive.Player;
import org.groovymud.object.registry.MudObjectAttendant;
import org.groovymud.object.registry.Registry;
import org.groovymud.shell.security.callbacks.ActionCallback;
import org.groovymud.shell.security.MudLoginModuleData;

import static org.groovymud.shell.security.callbacks.ActionType.CREATE;
import static org.groovymud.shell.security.loginsteps.CallBackDelegate.*;

public class CreatePlayer extends ActionLoginStep {


    public CreatePlayer(Registry registry, MudObjectAttendant mudObjectAttendant) {
        super(registry, mudObjectAttendant);
    }

    @Override
	public void execute(MudLoginModuleData data) throws IOException, UnsupportedCallbackException, FailedLoginException {
		if (data.getPlayer() == null) {
			Callback[] cb;
            ActionCallback createAction = new ActionCallback(data.getUsername(), CREATE, attendant, registry);
			cb = createCallbackArray(createAction);
			data.getCallbackHandler().handle(cb);
			data.setPlayer(createAction.getPlayer());
		}
		if (getNextStep() != null) {
			getNextStep().execute(data);
		}
	}

}
